﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Lswweb.Model;
using Lswweb.IDAL;
using Lswweb.Common;

namespace Lswweb.SqlServerDAL
{
    public class LogisticsCompanyDAL : ILogisticsCompanyDAL
    {
        #region 构造方法、字段

        private Builder<LogisticsCompanyInfo> builderDAL = new Builder<LogisticsCompanyInfo>("LogisticsCompany", "Id");

        public LogisticsCompanyDAL()
        {
            builderDAL.CreateModel += new ReaderBind<LogisticsCompanyInfo>(builderDAL_CreateModel);
            builderDAL.GetParameters += new ParametersBuilder<LogisticsCompanyInfo>(builderDAL_GetParameters);
        }

        IDataParameter[] builderDAL_GetParameters(LogisticsCompanyInfo t)
        {
            return new IDataParameter[]{
                DataAccess.CreateParameter("@Id",t.Id),
                DataAccess.CreateParameter("@CompanyName",t.CompanyName),
                DataAccess.CreateParameter("@Website",t.Website),
                DataAccess.CreateParameter("@State",t.State),
                DataAccess.CreateParameter("@CreateOn",t.CreateOn)
            };
        }

        LogisticsCompanyInfo builderDAL_CreateModel(Hashtable reader)
        {
            LogisticsCompanyInfo entity = new LogisticsCompanyInfo();

            entity.Id = reader.Get<int>("Id");
            entity.CompanyName = reader.Get<string>("CompanyName");
            entity.Website = reader.Get<string>("Website");
            entity.State = reader.Get<StatusType>("State");
            entity.CreateOn = reader.Get<DateTime>("CreateOn");

            return entity;
        }

        #endregion

        public LogisticsCompanyInfo FindById(int id)
        {
            return builderDAL.GetModel(id);
        }

        public IList<LogisticsCompanyInfo> FindAll()
        {
            return builderDAL.GetModels();
        }

        public void Add(LogisticsCompanyInfo entity)
        {
            builderDAL.Insert(
                entity,
                "CompanyName",
                "Website",
                "State",
                "CreateOn");
        }

        public void Update(LogisticsCompanyInfo entity)
        {
            builderDAL.Update(
                entity,
                "Id=@Id",
                "CompanyName",
                "Website",
                "State");
        }

        public void Delete(int id)
        {
            builderDAL.Delete(id);
        }
    }
}
